package com.javaEE.gamepigeon.controller;


import com.javaEE.gamepigeon.entity.User;
import com.javaEE.gamepigeon.service.UserService;
import com.javaEE.gamepigeon.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户信息接口")
public class UserController {

    @Autowired
    UserService userService;

    @ExceptionHandler
    public Result exceptionHandle(Exception e){
        return Result.error("用户操作错误！",e.getMessage());
    }

    @ApiOperation("获得当前用户的信息")
    @GetMapping("/query")
    public User getUserDetail(){
        return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }


    @ApiOperation(value = "修改当前用户的信息",notes = "这里微信的id和数据库id是无法修改的,修改学校，专业之类的不要通过对象修改，只能通过id来改！！")
    @PostMapping("/update")
    public Result updateUserDetail(User user){
        if(userService.updateUserDetail(user)){
            return Result.success("更新成功");
        }else {
            return Result.error("更新失败！");
        }
    }

}
